package test;

import test.bean.UserBean;

import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import java.awt.Font;
import javax.swing.JTextArea;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.DefaultComboBoxModel;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.Date;

public class FeedBackPanel extends JPanel {
    private JTable table;
    private UserBean user;

    /**
     * Create the panel.
     */
    public FeedBackPanel() {
        initialize();
    }
    public FeedBackPanel(UserBean user) {
        this.user = user;
        initialize();
    }

    public void initialize() {

        JScrollPane scrollPane_1 = new JScrollPane();

        JComboBox sendcomboBox = new JComboBox();
        sendcomboBox.setModel(new DefaultComboBoxModel(new String[]{"", "用户", "Krafton", "Cyber Connect2", "Entertainment", "Gearbox", "CD Projekt RED"}));

        JLabel FeedBack = new JLabel("反馈中心");
        FeedBack.setFont(new Font("宋体", Font.BOLD, 20));

        JLabel lblNewLabel = new JLabel("如果您对您购买的商品有什么建议或意见，");
        lblNewLabel.setFont(new Font("宋体", Font.PLAIN, 15));

        JLabel lblNewLabel_1 = new JLabel("均可在此向厂商提出，也可在此查看厂商的答复信息。\r\n");
        lblNewLabel_1.setFont(new Font("宋体", Font.PLAIN, 15));

        JLabel lblNewLabel_2 = new JLabel("反馈箱");
        lblNewLabel_2.setFont(new Font("宋体", Font.BOLD, 20));

        JScrollPane scrollPane = new JScrollPane();

        JComboBox receivecomboBox = new JComboBox();
        receivecomboBox.setModel(new DefaultComboBoxModel(new String[]{"", "用户", "Krafton", "Cyber Connect2", "Entertainment", "Gearbox", "CD Projekt RED"}));
        JTextArea textArea = new JTextArea();
        JButton searchbtn = new JButton("搜索");
        JButton sendbtn = new JButton("发送");
        JButton feedbackbtn = new JButton("我要反馈");
        searchbtn.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                String sender = (String) sendcomboBox.getSelectedItem();
                String receiver = (String) receivecomboBox.getSelectedItem();
                if (table.getSelectedRowCount() == 1) {
                    textArea.setText("");
                    textArea.append("时间：" + table.getValueAt(table.getSelectedRow(), 0) + "\n");
                    textArea.append("内容：\n" + table.getValueAt(table.getSelectedRow(), 3));
                } else {
                    for (int i = 0; i < table.getRowCount(); i++) {
                        if (table.getValueAt(i, 1) == receiver && table.getValueAt(i, 2) == sender) {
                            textArea.setText("");
                            textArea.append("时间：" + table.getValueAt(i, 0) + "\n");
                            textArea.append("内容：\n" + table.getValueAt(i, 3));
                        }
                    }
                }

            }
        });
        searchbtn.setFont(new Font("宋体", Font.PLAIN, 20));

        JLabel lblNewLabel_3 = new JLabel("发送者:");
        lblNewLabel_3.setFont(new Font("宋体", Font.PLAIN, 15));

        JLabel lblNewLabel_3_1 = new JLabel("接受者:");
        lblNewLabel_3_1.setFont(new Font("宋体", Font.PLAIN, 15));


        sendbtn.setEnabled(false);
        sendbtn.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                Date date = new Date();
                String str = textArea.getText();
                String sender = (String) sendcomboBox.getSelectedItem();
                String receiver = (String) receivecomboBox.getSelectedItem();
                DefaultTableModel tableModel = (DefaultTableModel) table.getModel();
                tableModel.addRow(new Object[]{date, sender, receiver, str});
                sendbtn.setEnabled(false);
                feedbackbtn.setEnabled(true);
                searchbtn.setEnabled(true);
                textArea.setText("（在此区域查看反馈或编写主体内容）");
                sendcomboBox.setEnabled(true);
            }
        });
        sendbtn.setFont(new Font("宋体", Font.PLAIN, 20));


        feedbackbtn.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                textArea.setText("");
                feedbackbtn.setEnabled(false);
                sendbtn.setEnabled(true);
                searchbtn.setEnabled(false);
                sendcomboBox.setSelectedIndex(1);
                sendcomboBox.setEnabled(false);
            }
        });
        feedbackbtn.setFont(new Font("宋体", Font.PLAIN, 15));
        GroupLayout groupLayout = new GroupLayout(this);
        groupLayout.setHorizontalGroup(
                groupLayout.createParallelGroup(Alignment.LEADING)
                        .addGroup(groupLayout.createSequentialGroup()
                                .addGap(23)
                                .addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
                                        .addGroup(groupLayout.createSequentialGroup()
                                                .addComponent(lblNewLabel, GroupLayout.PREFERRED_SIZE, 306, GroupLayout.PREFERRED_SIZE)
                                                .addContainerGap())
                                        .addGroup(groupLayout.createSequentialGroup()
                                                .addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
                                                        .addComponent(lblNewLabel_1, GroupLayout.PREFERRED_SIZE, 364, GroupLayout.PREFERRED_SIZE)
                                                        .addGroup(groupLayout.createSequentialGroup()
                                                                .addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
                                                                        .addComponent(scrollPane_1, GroupLayout.PREFERRED_SIZE, 452, GroupLayout.PREFERRED_SIZE)
                                                                        .addComponent(FeedBack, GroupLayout.PREFERRED_SIZE, 197, GroupLayout.PREFERRED_SIZE)
                                                                        .addComponent(lblNewLabel_2, GroupLayout.PREFERRED_SIZE, 148, GroupLayout.PREFERRED_SIZE))
                                                                .addPreferredGap(ComponentPlacement.UNRELATED, 38, Short.MAX_VALUE)
                                                                .addGroup(groupLayout.createParallelGroup(Alignment.TRAILING, false)
                                                                        .addGroup(groupLayout.createSequentialGroup()
                                                                                .addComponent(lblNewLabel_3)
                                                                                .addGap(18)
                                                                                .addComponent(sendcomboBox, GroupLayout.PREFERRED_SIZE, 110, GroupLayout.PREFERRED_SIZE)
                                                                                .addPreferredGap(ComponentPlacement.RELATED, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                                                                .addComponent(lblNewLabel_3_1, GroupLayout.PREFERRED_SIZE, 53, GroupLayout.PREFERRED_SIZE)
                                                                                .addPreferredGap(ComponentPlacement.UNRELATED)
                                                                                .addComponent(receivecomboBox, GroupLayout.PREFERRED_SIZE, 110, GroupLayout.PREFERRED_SIZE)
                                                                                .addGap(20))
                                                                        .addComponent(scrollPane, GroupLayout.PREFERRED_SIZE, 444, GroupLayout.PREFERRED_SIZE))
                                                                .addPreferredGap(ComponentPlacement.RELATED)
                                                                .addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
                                                                        .addComponent(feedbackbtn, GroupLayout.PREFERRED_SIZE, 112, GroupLayout.PREFERRED_SIZE)
                                                                        .addComponent(sendbtn, GroupLayout.PREFERRED_SIZE, 112, GroupLayout.PREFERRED_SIZE)
                                                                        .addComponent(searchbtn, GroupLayout.PREFERRED_SIZE, 112, GroupLayout.PREFERRED_SIZE))))
                                                .addContainerGap())))
        );
        groupLayout.setVerticalGroup(
                groupLayout.createParallelGroup(Alignment.TRAILING)
                        .addGroup(groupLayout.createSequentialGroup()
                                .addGap(49)
                                .addComponent(FeedBack, GroupLayout.PREFERRED_SIZE, 32, GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(ComponentPlacement.RELATED)
                                .addComponent(lblNewLabel, GroupLayout.PREFERRED_SIZE, 30, GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(ComponentPlacement.RELATED)
                                .addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
                                        .addGroup(groupLayout.createSequentialGroup()
                                                .addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
                                                        .addGroup(groupLayout.createSequentialGroup()
                                                                .addGap(49)
                                                                .addComponent(lblNewLabel_2)
                                                                .addPreferredGap(ComponentPlacement.RELATED))
                                                        .addGroup(Alignment.TRAILING, groupLayout.createSequentialGroup()
                                                                .addGap(18)
                                                                .addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
                                                                        .addComponent(lblNewLabel_3_1, GroupLayout.PREFERRED_SIZE, 37, GroupLayout.PREFERRED_SIZE)
                                                                        .addComponent(lblNewLabel_3, GroupLayout.PREFERRED_SIZE, 37, GroupLayout.PREFERRED_SIZE)
                                                                        .addComponent(receivecomboBox, GroupLayout.PREFERRED_SIZE, 34, GroupLayout.PREFERRED_SIZE)
                                                                        .addComponent(sendcomboBox, GroupLayout.PREFERRED_SIZE, 34, GroupLayout.PREFERRED_SIZE))
                                                                .addGap(15)))
                                                .addGroup(groupLayout.createParallelGroup(Alignment.TRAILING)
                                                        .addComponent(scrollPane_1, Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 322, GroupLayout.PREFERRED_SIZE)
                                                        .addGroup(Alignment.LEADING, groupLayout.createParallelGroup(Alignment.TRAILING, false)
                                                                .addGroup(Alignment.LEADING, groupLayout.createSequentialGroup()
                                                                        .addComponent(searchbtn, GroupLayout.PREFERRED_SIZE, 28, GroupLayout.PREFERRED_SIZE)
                                                                        .addPreferredGap(ComponentPlacement.RELATED, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                                                        .addComponent(feedbackbtn, GroupLayout.PREFERRED_SIZE, 28, GroupLayout.PREFERRED_SIZE)
                                                                        .addGap(18)
                                                                        .addComponent(sendbtn, GroupLayout.PREFERRED_SIZE, 28, GroupLayout.PREFERRED_SIZE))
                                                                .addComponent(scrollPane, Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 321, GroupLayout.PREFERRED_SIZE))))
                                        .addComponent(lblNewLabel_1, GroupLayout.PREFERRED_SIZE, 30, GroupLayout.PREFERRED_SIZE))
                                .addContainerGap(78, Short.MAX_VALUE))
        );


        textArea.setFont(new Font("Monospaced", Font.PLAIN, 15));
        textArea.setText("（在此区域查看反馈或编写主体内容）");
        scrollPane.setViewportView(textArea);

        table = new JTable();
        table.setFont(new Font("宋体", Font.PLAIN, 15));
        table.setRowHeight(30);
        table.setModel(new DefaultTableModel(
                new Object[][]{
                        {"2020.8.8", "Krafton", "\u7528\u6237", ""},
                        {"2021.9.7", "Cyber Connect2", "\u7528\u6237", ""},
                },
                new String[]{
                        "\u65F6\u95F4", "\u63A5\u53D7\u8005", "\u53D1\u9001\u8005", "\u5185\u5BB9"
                }
        ));
        table.getColumnModel().getColumn(0).setPreferredWidth(120);
        table.getColumnModel().getColumn(1).setPreferredWidth(120);

        scrollPane_1.setViewportView(table);
        setLayout(groupLayout);
    }


}
